package com.utils;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

public class DBUtils {

    private static DataSource source;

    public static DataSource getSource() {
        return source;
    }

    static {
        try {
            InputStream inputStream = DBUtils.class.getResourceAsStream("/db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            source = new DruidDataSource();
            ((DruidDataSource) source).setUsername(properties.getProperty("jdbc.username"));
            ((DruidDataSource) source).setPassword(properties.getProperty("jdbc.password"));
            ((DruidDataSource) source).setUrl(properties.getProperty("jdbc.url"));
            ((DruidDataSource) source).setDriverClassName(properties.getProperty("jdbc.driver"));
            ((DruidDataSource) source).setMaxActive(20);
            ((DruidDataSource) source).setMinIdle(3);
            ((DruidDataSource) source).setInitialSize(10);
            ((DruidDataSource) source).setMaxWait(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
